package cn.gs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.gs.domain.Customer;
import cn.gs.domain.LinkMan;
import cn.gs.utils.HibernateUtil;

@Component(value = "selectCustomerDao")
@Scope("prototype")
public class SelectCustomerImpl implements SelectCustomerDao {

	Session session = HibernateUtil.openSession();
	Transaction transaction = session.getTransaction();

	@Override
	public List<Customer> selectCustomer(String cust_name, String cust_linkman, int page) {

		boolean custlinkman;
		boolean custname;
		if (cust_linkman==null||cust_linkman.isEmpty()) {
			custlinkman = true;
		} else {
			custlinkman = false;
		}
		if (cust_name==null||cust_name.isEmpty()) {
			custname = true;
		} else {
			custname = false;
		}
		if (custlinkman && custname) {
			transaction.begin();
			Query query = session.createQuery("from Customer");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List<Customer> list = query.list();
			transaction.commit();
			session.close();
			return list;
		}

		if (custname && (!custlinkman)) {
			transaction.begin();
			Query query = session.createQuery("from Customer where cust_linkman like ?");
			query.setParameter(0, "%" + cust_linkman + "%");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List list = query.list();
			Customer c = (Customer) list.get(0);
			transaction.commit();
			session.close();
			return list;
		}

		if ((!custname) && custlinkman) {
			transaction.begin();
			Query query = session.createQuery("from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List list = query.list();
			Customer c = (Customer) list.get(0);
			transaction.commit();
			session.close();
			return list;
		}

		if ((!custname) && (!custlinkman)) {
			transaction.begin();
			Query query = session.createQuery("from Customer where cust_name like ? and cust_linkman like ?");
			query.setParameter(0, "%" + cust_name + "%");
			query.setParameter(1, "%" + cust_linkman + "%");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List list = query.list();
			Customer c = (Customer) list.get(0);
			transaction.commit();
			session.close();
			return list;
		}
		return null;

	}

	@Override
	public List<Customer> selectPageCount(String cust_name, String cust_linkman) {
		boolean custlinkman;
		boolean custname;
		if (cust_linkman==null||cust_linkman.isEmpty()) {
			custlinkman = true;
		} else {
			custlinkman = false;
		}
		if (cust_name==null||cust_name.isEmpty()) {
			custname = true;
		} else {
			custname = false;
		}
		if (custlinkman && custname) {
			transaction.begin();
			Query query = session.createQuery("select count(l) from Customer l");
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}
		if(custname&&(!custlinkman)) {
			transaction.begin();
			Query query=session.createQuery("select count(c) from Customer c where cust_linkman like ?");
			query.setParameter(0, "%"+cust_linkman+"%");
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}
		
		if ((!custname) && custlinkman) {
			transaction.begin();
			Query query = session.createQuery("select count(c) from Customer c where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}

		if ((!custname) && (!custlinkman)) {
			transaction.begin();
			Query query = session.createQuery("select count(c) from Customer c where cust_name like ? and cust_linkman like ?");
			query.setParameter(0, "%" + cust_name + "%");
			query.setParameter(1, "%" + cust_linkman + "%");
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}				
		return null;
	}

}
